﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MMITDomain.Abstract;
using MMITDomain.Entities;

namespace MMITDomain.Concrete
{
    public class EFCustomerRepository : ICustomerRepository
    {
        private EFDbContext dbContext = new EFDbContext();


        #region ICustomerRepository Members

        public IQueryable<Customer> Customers
        {
            get
            {
                return dbContext.Customers;
            }
        }

        public void SaveCustomer(Customer objCustomer)
        {
            if (objCustomer.Id == 0)
            {
                dbContext.Customers.Add(objCustomer);  
            }

            dbContext.SaveChanges();
        }

        public void DeleteCustomer(Customer objCustomer)
        {
            dbContext.Customers.Remove(objCustomer);

            dbContext.SaveChanges();
        }

        public Customer GetCustomerById(int iCustomerId)
        {
            return dbContext.Customers.Find(iCustomerId);
        }

        public Customer GetCustomerByMSISDN(string strCustomerMSISDN)
        {
            return dbContext.Customers.Where(c => c.Mobile == strCustomerMSISDN)
                                        .SingleOrDefault<Customer>();
        }

        public Customer GetCustomerByUsername(string strUsername)
        {
            return dbContext.Customers.Where(c => c.Username == strUsername)
                .SingleOrDefault<Customer>();
        }

        #endregion
    }
}
